package com.study.rabbitmq.producer;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * 消费者：
 * 消费模式 1.拉 pull 2.push 推
 * 可靠性 ack
 */
public class Recv {
	private final static String QUEUE_NAME = "tanQueue";
	private final  static  String EXCHANGE_NAME = "tanExchange";

	private final  static  String ROUTING_KEY = "tan";

	public static void main(String[] argv) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("192.168.20.68");
		factory.setPort(5672);
		factory.setUsername("leke");
		factory.setPassword("leke@@@");

		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,ROUTING_KEY);

		Consumer consumer = new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

				String message = new String(body);
				System.out.println(" [x] Received '" + message + "'");

				  channel.basicAck(envelope.getDeliveryTag(), false);
			}
		};

		channel.basicConsume(QUEUE_NAME,false,consumer);

//        while (true){
//        	channel.basicGet();
//		}
	}
}
